package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.DataBaseConnection;

import bean.Detalle_guia_de_entrada;

import dao.Entidad;

public class MySQLDetalle_guia_de_entradaDAO implements Entidad<Detalle_guia_de_entrada>{

	@Override
	public Detalle_guia_de_entrada insert(Detalle_guia_de_entrada t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO detalle_guia_de_entrada(id_producto,id_guia_entrada,";
			sql = sql + "cantidad) VALUES(?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_producto());
			ps.setInt(2, t.getId_guia_entrada());
			ps.setInt(3, t.getCantidad());
			ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public int update(Detalle_guia_de_entrada t) {
		Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE detalle_guia_de_entrada SET cantidad = ?";
			sql = sql + "WHERE id_producto= ?, id_guia_entrada=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getCantidad());
			ps.setInt(2, t.getId_producto());
			ps.setInt(3, t.getId_guia_entrada());
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public int delete(Detalle_guia_de_entrada t) {
		Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM detalle_guia_de_entrada WHERE id_producto= ?, id_guia_entrada=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_producto());
			ps.setInt(2, t.getId_guia_entrada());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public Detalle_guia_de_entrada findById(Detalle_guia_de_entrada t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM detalle_guia_de_entrada WHERE id_producto= ?, id_guia_entrada=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_producto());
			ps.setInt(2, t.getId_guia_entrada());
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				t.setId_producto(rs.getInt("id_producto"));
				t.setId_guia_entrada(rs.getInt("id_guia_entrada"));
				t.setCantidad(rs.getInt("cantidad"));
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public List<Detalle_guia_de_entrada> listAll() {
		List<Detalle_guia_de_entrada> detalle_guias = new ArrayList<Detalle_guia_de_entrada>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM detalle_guia_de_entrada WHERE id_producto= ?, id_guia_entrada=?";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Detalle_guia_de_entrada t = new Detalle_guia_de_entrada();
				t.setId_producto(rs.getInt("id_producto"));
				t.setId_guia_entrada(rs.getInt("id_guia_entrada"));
				t.setCantidad(rs.getInt("cantidad"));
				detalle_guias.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return detalle_guias;
	}

	@Override
	public List<Detalle_guia_de_entrada> listAllCustom(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}

}
